#include <QDataStream>
#include "chatmessage.h"

ChatMessage::ChatMessage(int newID, QString newMsg)
{
        userID = newID;
        msg = newMsg;
}

ChatMessage::ChatMessage(QByteArray bytes)
{
        QDataStream sin(bytes);

        quint8 tmp;
        sin >> tmp;

        sin >> userID;

        char* newData = NULL;
        sin >> newData;
        msg = QString(newData);
        delete [] newData;
}

QByteArray ChatMessage::toBytes()
{
        QByteArray bytes;
        QDataStream sout(&bytes, QIODevice::WriteOnly);

        sout << (quint8)Message::CHAT << userID << msg.toAscii();

        return bytes;
}

QByteArray ChatMessage::toBytesSys()
{
        QByteArray bytes;
        QDataStream sout(&bytes, QIODevice::WriteOnly);

        sout << (quint8)Message::SYS << userID << msg.toAscii();

        return bytes;
}
